package org.eden.rpc.service.serviceImpl.blog;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;
import org.eden.rpc.common.entity.dao.Article;
import org.eden.rpc.common.service.blog.ArticleService;
import org.eden.rpc.service.dao.SqlSessionFactoryHolder;
import org.eden.rpc.service.dao.blog.ArticleMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ArticleServiceImpl implements ArticleService {

    private final static Logger logger = LoggerFactory.getLogger(ArticleServiceImpl.class);

    @Override
    public String getArticles() {
        return "get all articles";
    }

    @Override
    public Long addArticle(Article article) {
        try (SqlSession sqlSession = SqlSessionFactoryHolder.get().openSession(true)) {
            ArticleMapper articleMapper = sqlSession.getMapper(ArticleMapper.class);
            if (articleMapper.insertArticle(article) > 0) {
                return article.getId();
            }
        } catch (PersistenceException e) {
            logger.error(e.getMessage());
        }
        return -1L;
    }
}
